package framework.common.takephoto;

import android.net.Uri;
import android.os.Environment;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;


/**
 * - 支持通过相机拍照获取图片
 * - 支持从相册选择图片
 * - 支持从文件选择图片
 * - 支持多图选择
 * - 支持批量图片裁切
 * - 支持批量图片压缩
 * - 支持对图片进行压缩
 * - 支持对图片进行裁剪
 * - 支持对裁剪及压缩参数自定义
 * - 提供自带裁剪工具(可选)
 * - 支持智能选取及裁剪异常处理
 * - 支持因拍照Activity被回收后的自动恢复
 * Author: crazycodeboy
 * Date: 2016/9/21 0007 20:10
 * Version:4.0.0
 * 技术博文：http://www.cboy.me
 * GitHub:https://github.com/crazycodeboy
 * Eamil:crazycodeboy@gmail.com
 */
public class CustomHelper{
    public static void onClick(TakePhoto takePhoto, int postion) {
        File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        configCompress(takePhoto);
        configTakePhotoOption(takePhoto);
        switch (postion){
            case 1:
                int limit= 1;
                if(limit>1){
                        takePhoto.onPickMultipleWithCrop(limit,getCropOptions());
                    return;
                }
                   // if(rgCrop.getCheckedRadioButtonId()==R.id.rbCropYes){
                        //takePhoto.onPickFromGalleryWithCrop(imageUri,getCropOptions());
//                    }else {
                        takePhoto.onPickFromGallery();
//                    }
                break;
            case 0:
                    takePhoto.onPickFromCapture(imageUri);
                break;
            default:
                break;
        }
    }
    private static void configTakePhotoOption(TakePhoto takePhoto){
        TakePhotoOptions.Builder builder=new TakePhotoOptions.Builder();
        builder.setWithOwnGallery(true);
        takePhoto.setTakePhotoOptions(builder.create());

    }
    private static void configCompress(TakePhoto takePhoto){
        int maxSize= 1024000;
        int width= 800;
        int height= 800;
        boolean showProgressBar=false;
        boolean enableRawFile = true;
        CompressConfig config;
            LubanOptions option=new LubanOptions.Builder()
                    .setMaxHeight(height)
                    .setMaxWidth(width)
                    .setMaxSize(maxSize)
                    .create();
            config=CompressConfig.ofLuban(option);
            config.enableReserveRaw(enableRawFile);
        takePhoto.onEnableCompress(config,showProgressBar);


    }
    private static CropOptions getCropOptions(){
        int height= 800;
        int width= 800;
        boolean withWonCrop= false;

        CropOptions.Builder builder=new CropOptions.Builder();

        //if(rgCropSize.getCheckedRadioButtonId()==R.id.rbAspect){
      //      builder.setAspectX(width).setAspectY(height);
//        }else {
          builder.setOutputX(width).setOutputY(height);
//        }
        builder.setWithOwnCrop(withWonCrop);
        return builder.create();
    }

}
